<?php

namespace app\api\controller;

use app\BaseController;
use app\model\CouponOrder;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Label\Margin\Margin;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use think\facade\Db;
use EasyWeChat\Factory;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        echo 'hey you!';
        exit;
        $ai = new \baidu\Ai();
        // $res = $ai->getAccessToken();
        // echo '<pre>';
        // print_r($res);
        $accessToken = '24.3ce8f2a9cbbb193a62d1fc59f46aefac.2592000.1623305510.282335-24145490';
        $res = $ai->getVerifyToken($accessToken);
        $verifyToken = 'ghAeABL4NWlx2V85xs04Mbw0';
        $verifyToken = '1dcFHPeJX9AKFAQrNErARrjP';
        echo '<pre>';
        print_r($res);
    }

    public function go()
    {
        exit;
        $verifyToken = 'ghAeABL4NWlx2V85xs04Mbw0';
        $verifyToken = '1dcFHPeJX9AKFAQrNErARrjP';
        $successUrl = urlencode('https://www.baidu.com');
        $failedUrl = urlencode('https://www.jd.com');
        $url = 'https://brain.baidu.com/face/print/?token=' . $verifyToken . '&successUrl=' . $successUrl . '&failedUrl=' . $failedUrl;
        echo $url;
    }

    public function detail()
    {
        exit;
        $accessToken = '24.3ce8f2a9cbbb193a62d1fc59f46aefac.2592000.1623305510.282335-24145490';
        $verifyToken = 'ghAeABL4NWlx2V85xs04Mbw0';
        $verifyToken = '1dcFHPeJX9AKFAQrNErARrjP';
        $ai = new \baidu\Ai();
        $res = $ai->resultDetail($accessToken, $verifyToken);
        echo '<pre>';
        print_r($res);
    }

    /**
     * 守兔影票测试回调
     */
    public function taototoOrderHandle()
    {
        $service = new \app\common\service\Taototo;
        return $service->orderHandle(input('post.'));
    }

    /**
     *  权益测试支付成功回调
     */
    public function paySuccess()
    {
        $id = input("id");
        $log = Db::name('coupon_pay_log')->where('id', $id)->find();
//        $result = CouponOrder::paySuccess($log);
        $result = CouponOrder::paySuccessNew($log);
        return $result;
    }

    public function jssdk()
    {
        $app = Factory::officialAccount([
            'app_id' => env('wechat.app_id'),
            'secret' => env('wechat.secret'),

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ]);
        $url = input('url');
        $res = $app->jssdk->setUrl($url)->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'], false, false, false, ['wx-open-launch-weapp']);

        return success('操作成功', $res);
    }


    public function testJssdk()
    {

        $config = Db::name('config')
            ->where('type', 'wechat')
            ->column('value', 'field');

        $app = Factory::officialAccount([
            'app_id' => $config['app_id'],
            'secret' => $config['app_secret'],

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ]);
        $url = request()->domain() . '/api/index/testJssdk';

        $res = $app->jssdk->setUrl($url)->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'], false, false, false, ['wx-open-launch-weapp']);


        if (!empty($res['jsApiList'])) {
            $res['jsApiList'] = implode(',', $res['jsApiList']);
            $res['url'] = $url;
        }

        return View::fetch('', $res);

    }





}
